<?php
if(cmd("code") or cmd("ecode",50)){
  $codeResponse="";
  if(!function_exists("SEqualS")){
    function SEqualS($fname){
      global $codeResponse;
      $codeResponse=preg_replace("/$fname (.*?)=(.*?)/","$fname $1 == $2",$codeResponse);
      $codeResponse=preg_replace("/$fname (.*?) equals (.*?)/","$fname $1 == $2",$codeResponse);
      $codeResponse=preg_replace("/$fname (.*?) not equals (.*?)/","$fname $1 == $2",$codeResponse);
      $codeResponse=preg_replace("/$fname (.*?) is equal to (.*?)/","$fname $1 == $2",$codeResponse);
      $codeResponse=preg_replace("/$fname (.*?) is not equal to (.*?)/","$fname $1 == $2",$codeResponse);
      $codeResponse=preg_replace("/$fname (.*?) do (.*?) end/","$fname ($1){ $2 }",$codeResponse);
    }
  }
  //$codeResponse=implode(" ",$paramA);
  $codeResponse=trim($msg_params);
  $codeResponse=str_replace("password","\"password\"",$codeResponse);
  //$codeResponse=preg_replace("/(.*?)\((.*?)\)/","[$1 $2]",$codeResponse);
  SEqualS("if");
  SEqualS("while");

  $blockedCommands=Array("system","die","exec","system_o");

  $codeResponse=preg_replace("/global (.*?);/","",$codeResponse);
  
  $codeResponse=preg_replace("/else if (.*?) equals (.*?)/","else if $1 == $2",$codeResponse);
  $codeResponse=preg_replace("/else if (.*?) not equals (.*?)/","else if $1 == $2",$codeResponse);
  $codeResponse=preg_replace("/else if (.*?) is equal to (.*?)/","else if $1 == $2",$codeResponse);
  $codeResponse=preg_replace("/else if (.*?) is not equal to (.*?)/","else if $1 == $2",$codeResponse);
  $codeResponse=preg_replace("/else if (.*?) do (.*?)/","} else if ($1){ $2 ",$codeResponse);
  $codeResponse=preg_replace("/if(.*?) else (.*?)/","if$1 } else { ",$codeResponse);


  $codeResponse=preg_replace("/\(not /","(!",$codeResponse);
  $codeResponse=preg_replace("/\[not /","(!",$codeResponse);

  $codeResponse=preg_replace("/\[(.*?)=(.*?)\]/","$$1=$2;",$codeResponse);
  $codeResponse=preg_replace(Array("/\[print \"(.*?)\"\]/","/\[print \'(.*?)\'\]/"),Array("return \"$1\";","return '$1';"),$codeResponse);
  $codeResponse=preg_replace("/\[print (.*?)\]/","return $$1;",$codeResponse);
  //$codeResponse=preg_replace("/\[empty (.*?)\]/","empty($$1)",$codeResponse);
  $codeResponse=preg_replace("/\[empty (.*?)\]/","empty($1)",$codeResponse);

  $codeResponse=preg_replace("/if \(([a-zA-Z0-9]+)/", "if ($$1",$codeResponse);

  $codeResponse=preg_replace("/==(.*?)=/","==",$codeResponse);

  if(empty($codeResponse)){
    $codeResponse='I can\'t code that! Sorry!';
  }
  if(cmd("code")){
    reply($codeResponse);
  } else if(command("ecode",true)){
    $text=eval($codeResponse);
    if(empty($text)){
      $text='No output from code.';
    }
    reply($text);
  }
}
?>